<template>
	<view class="friend-video-box bottomBar">
		<scroll-view class="scroll-wp" :scroll-y="true" @scrolltolower="$u.debounce(scrolltolower,300)"
			:lower-threshold="100">
			<view class="scroll-item" v-if="dataList && dataList.length>0">
				<view class="video-item" v-for="(list,index) in dataList" :key="index">
					<u--image showLoading radius="6rpx" :src="list.cover?list.cover:list.photo" width="100%"
						height="506rpx" mode="aspectFill" @click="towxh5Video(index)" :key="index"></u--image>
					<!-- <view class="playback flex_start_center">
						<image class="playbackIcon"
							src="https://file.casugn.com/storecompcard//VfXFFdWH2p73346bb4d55aad4a06dbea38f648d11b6c_1681958946196.png"
							mode="aspectFit">
						</image>
						{{list.praise||''}}
					</view> -->
					<view class="title u-line-1">
						{{list.title}}
					</view>
				</view>
			</view>
			<nodata style="min-height:400rpx" v-else
				nodataImgUrl="https://file.casugn.com/storecompcard/temp/空页面@2x(6)_1681114072965.png"
				nodataText="暂无信息"></nodata>
		</scroll-view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				dataList: [],
				pageNo: 1,
				total: 0,
				pageSize: 10,
				firstLoad: null,
			}
		},
		props: ['openId', 'cardId', 'currentOpenId'],
		methods: {
			// 获取视频列表
			getVideoLists() {
				this.dataList = []
				this.pageNo = 1
				let data = {
					openId: this.openId,
					cardId: this.cardId,
					pageNo: this.pageNo,
					pageSize: this.pageSize,
					status: 1,
					currentOpenId: this.currentOpenId
				}
				this.$api.getMediaList(data).then(res => {
					if (res.success && res.result.records) {
						console.log(res.result.records, 'res.result.records');
						this.dataList = res.result.records
						this.total = res.result.total
					} else {
						this.dataList = []
						this.total = 0
					}
				})
			},
			towxh5Video(index) {
				uni.removeStorageSync('dataList');
				let list = []
				let videoNumber = 36; //（请输入 6 的倍数，来控制 DOM 节点的总视频数量）
				let dotNumber = videoNumber / 2;
				let inf = (index + 1) - dotNumber;
				console.log('点击了第' + (index + 1) + '个视频.');
				let beforeLocation = 0;
				let afterLocation = 0;
				let location = 0;
				if (inf <= 0) {
					beforeLocation = 0;
					location = index;
					for (let i = 0; i <= index; i++) {
						list.push(this.dataList[i]);
					}
				} else {
					beforeLocation = inf;
					location = index - inf;
					for (let i = inf; i <= index; i++) {
						list.push(this.dataList[i]);
					}
				}
				let onf = this.dataList.length - (index + 1)
				console.log('下方视频数：' + onf);
				if (onf <= dotNumber) {
					if (onf !== 0) {
						afterLocation = this.dataList.length - 1;
						for (let i = index + 1; i < this.dataList.length; i++) {
							list.push(this.dataList[i]);
						}
					} else {
						afterLocation = this.dataList.length - 1;
					}
				} else {
					afterLocation = index + dotNumber;
					for (let i = index + 1; i <= index + dotNumber; i++) {
						list.push(this.dataList[i]);
					}
				}
				console.log('总视频数：' + list.length);
				console.log('\n\n第一个视频下标：' + beforeLocation + '.\n' + '最后一个视频下标：' + afterLocation + '.\n\n');
				uni.setStorageSync("dataList", list);
				uni.navigateTo({
					url: `/pagesCard/friendInfoVideo/friendInfoVideo?option=${location}&page=${this.pageNo}&index=${index}&openId=${this.openId}&cardId=${this.cardId}`
				})
			},
			async scrolltolower() {
				if (this.firstLoad && (this.total === this.dataList.length)) return
				uni.showLoading({})
				this.pageNo++;
				let payload = {
					openId: this.openId,
					cardId: this.cardId,
					pageNo: this.pageNo,
					pageSize: this.pageSize,
					status: 1,
					currentOpenId: this.currentOpenId
				}
				let res = await this.$api.getMediaList(payload)
				uni.hideLoading()
				if (res.success) {
					let msg = res.result.records.map(item => {
						item._id = item.id
						return item
					})
					msg = msg.filter(item => {
						return item.openId == this.openId
					})
					this.dataList = this.pageNo === 1 ? msg : this.dataList.concat(msg)
					this.total = res.result.total
					if ((this.total === this.dataList.length) && this.firstLoad) this.$showToast('已全部加载')
				} else {
					setTimeout(() => {
						this.$showToast(res.message)
					}, 600)
				}
				this.firstLoad = true
			},
		}
	}
</script>

<style lang="scss" scoped>
	.friend-video-box {
		.scroll-wp {
			width: 100%;
			height: calc(100vh - 400rpx);
			padding: 0 20rpx;
			box-sizing: border-box;

			.scroll-item {
				display: flex;
				flex-direction: row;
				flex-wrap: wrap;
				justify-content: space-between;
			}

			.video-item {
				width: 48%;
				height: 506upx;
				margin-top: 5upx;
				margin-left: 5upx;
				margin-bottom: 24rpx;
				position: relative;

				.playback {
					height: 68rpx;
					position: absolute;
					bottom: 0;
					left: 0;
					color: #fff;
					font-size: 24rpx;

					.playbackIcon {
						width: 32rpx;
						height: 32rpx;
						margin: 0 6rpx 0 16rpx;
					}
				}

				.title {
					box-sizing: border-box;
					width: 100%;
					height: 68rpx;
					line-height: 68rpx;
					position: absolute;
					bottom: 0;
					left: 0;
					font-size: 24rpx;
					text-align: center;
					color: #fff;
					background-color: rgba(0, 0, 0, 0.2);
				}
			}

		}
	}
</style>